import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Solution349 {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set=new HashSet<>();
        for (int i : nums1) {
            set.add(i);
        }
        Set<Integer> ans=new HashSet<>();
        for (int i : nums2) {
            if (set.contains(i)){
                ans.add(i);
            }
        }
        int[] a=new int[ans.size()];
        int c=0;
        for (Integer an : ans) {
            a[c]=an;
            c++;
        }
        return a;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution349().intersection(new int[]{1,2,2,1},new int[]{2,2})));
    }
}
